<?php
namespace cubox;

class Check{
    
    public $title;
    private $checkList;
    private $defaultValue;

    public $message ;
    public $value ;
    public function __construct($title='',$config =  array())
    {
        $this->title = $title;
        
    }

    public function default($value):Check{
        $this->defaultValue = $value;
        return $this;
    }

 

    public function type($type,$message = '类型错误'):Check{
        $this->checkList['type'] = array('type' => $type,'message' => $message);
        return $this;
    }

    public function required($message = '不能为空'):Check{

        $this->checkList['required'] = ['message'=> $message];
        return $this;
    }

    public function reg($reg,$message='格式不正确'):Check{
        $this->checkList['reg'] = ['reg' => $reg,'message' => $message];
        return $this;
    }

    public function minLength(int $length,string $message=''):Check{
        if (empty($message)){
            $message = '长度不能小于'. $length;
        }
        $this->checkList['minLength'] = ['length' => $length,'message' => $message];
        return $this;
    }

    public function maxLength(int $length,string $message=''):Check{
        
        if (empty($message)){
            $message = '长度大于能小于'. $length;
        }

        $this->checkList['maxLength'] = ['length' => $length,'message' => $message];
        return $this;
    }

    public function min(int | float $value,$message=''):Check{

        if (empty($message)){
            $message = '不能小于'. $value;
        }

        $this->checkList['min'] = ['value' => $value,'message' => $message];
        return $this;
    }

    public function max(int | float $value,$message=''):Check{

        if (empty($message)){
            $message = '不能大于'. $value;
        }

        $this->checkList['max'] = ['value' => $value,'message' => $message];
        return $this;
    }

    public function check($value):bool{
        
        if (isset($this->checkList['required'])){
            if ($value==='' || $value===null){
                throw new \Exception( $this->title . $this->checkList['required']['message']);
                return false;
            }
        }else{
            if (empty($value)){
                
                $this->value =  $this->defaultValue;
                 
                return true;
            }
        }
        
        if (isset($this->checkList['type'])){

            $type = $this->checkList['type']['type'];
            $msg =  $this->title . $this->checkList['type']['message']; 
            switch ($type){
                case 'int':
                    if (!is_numeric($value)){
                        throw new \Exception($msg);
                        return false;
                    }

                    $value = (int)$value;
                break;
                case 'float':
                    if (!is_numeric($value)){
                        throw new \Exception($msg);
                        return false;
                        
                    }

                    $value = (float)$value;
                break;
                case 'bool':

                    if ($value === 'true' || $value === '1' || $value === 1 || $value === 'false' || $value === '0' || $value === 0){
                        if ($value == 'true' || $value == '1' || $value == 1){
                            $value  = true;
                        }else{
                            $value  = false;
                        }
                    }else{
                        throw new \Exception($msg);
                        return false;
                    }
                break;
                case 'string':
                    if (!is_string($value)){
                        throw new \Exception($msg);
                        return false;
                    }
                break;
                
            }
        }

        $this->value = $value;
        
        if (is_array($this->checkList)){
            foreach($this->checkList as $key => $check){
                switch($key){
                    case 'max':
                        if ($value >= $check['max']){
                            throw new \Exception( $this->title . $check['message']);
                            return false;
                        }
                    break;

                    case'min':
                        if ($value <= $check['min']){
                            throw new \Exception( $this->title .$check['message']);
                            return false;
                        }
                    break;

                    case 'minLength':
                        if (strlen($value) <= $check['length']){
                            throw new \Exception( $this->title . $value['message']);
                            return false;
                        }
                    break;

                    case'maxLength':
                        if (strlen($value) >= $check['length']){
                            throw new \Exception( $this->title . $value['message']);
                            return false;
                        }
                    break;

                    case 'reg':
                        if (!preg_match($check['reg'], $value)){
                            throw new \Exception( $this->title . $check['message']);
                            return false;
                        }
                    break;
                    
                }
            }
        }

        return true;
    }

    public function getConfig():array{
        return $this->checkList;
    }

    private function error($message){
       $this->message = $this->title . $message;
    }
}